package com.cloud.api.gateway.filter;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
/**
 * 过滤器异常处理，要在过滤器中的run加上try catch并设置参数
 * @author Administrator
 *
 */
@Component
public class ErrorFilter extends ZuulFilter {
	
	private static Logger logger=LoggerFactory.getLogger(ErrorFilter.class);

	@Override
	public Object run() {
		RequestContext ctx=RequestContext.getCurrentContext() ;
		Throwable throwable = ctx.getThrowable();
		logger.error("this a error filter :{}",throwable.getCause().getMessage());
		ctx.set("error.status_code",HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		ctx.set("error.exception",throwable.getCause());
		return null;
	}

	@Override
	public boolean shouldFilter() {
		return true;
	}

	@Override
	public int filterOrder() {
		return 10;
	}

	@Override
	public String filterType() {
		return "error";
	}

}
